package com.celebracion.user;

import java.util.List;

import com.celebracion.R;
import com.celebracion.util.DBUtil;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.celebracion.model.EventoModel;

public class VisorEventosAdapter extends BaseAdapter {

	private List<EventoModel> eventos;
	private Context context;
	
	public VisorEventosAdapter(List<EventoModel> eventos,Context mContext){
		this.eventos=eventos;
		this.context=mContext;
	}
	@Override
	public int getCount() {
		return eventos.size();
		
	}

	@Override
	public Object getItem(int position) {
		return eventos.get(position);
	}

	@Override
	public long getItemId(int position) {
		return eventos.get(position).getId();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		  EventoModel evento = eventos.get(position);

	        View v = View.inflate(context, R.layout.evento_row_adapter, null);

	        TextView tvRowDireccion = (TextView)v.findViewById(R.id.tvRowDireccion);
	        tvRowDireccion.setText(evento.getDireccion());
	        
	        TextView tvRowNombre = (TextView)v.findViewById(R.id.tvRowNombre);
	        tvRowNombre.setText(evento.getNombre());
	        
	        Button btnEliminar=(Button)v.findViewById(R.id.btRowEliminar);
	        btnEliminar.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					DBUtil.getInstance(VisorEventosAdapter.this.context).deleteEvento(eventos.get(position).getId());
					eventos.remove(position);
					notifyDataSetChanged();
				}});
	        
	        Button btnActivar=(Button)v.findViewById(R.id.btRowActivar);
	        if(DBUtil.getInstance(context).getEventoActivo().getId()==-1){
				
	        btnActivar.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					if(DBUtil.getInstance(VisorEventosAdapter.this.context).activarEvento(eventos.get(position).getId())){
						eventos.remove(position);
						notifyDataSetChanged();
					}else
						Toast.makeText(VisorEventosAdapter.this.context, R.string.noQuedanActivaciones, Toast.LENGTH_LONG).show();
				}});
	        }else{
	        	btnActivar.setClickable(false);
	        	btnActivar.setVisibility(View.GONE);
	        }
	        
	        return v;
	}
	
	
	

}
